<template>
  <div>
    <TopFilter
      ref="TopFilters"
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandler"
      @onCollapse="collapseHandle"
    />
    <FilterTable
      ref="table"
      columnsRef="lxxCollect"
      :columns="columns"
      :dataSource="tableDataList"
      :isMemoryPagination="true"
      :isExportExcel="true"
      :pageSize="20"
       exportFileName="客户流失汇总.xls"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 99,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import dictCode from '@/utils/dictCode';
import { getWxlxSelect } from '@/api/repair/repairReception/customer';
import { getCustomerSummary, getCustomerChurnBackSummary } from '@/api/repair/queryStatistics/queryAccounts';
export default {
  name: 'LHMHoursStatisticsSummary',
  components: {},
  mixins: [dictionary],
  props: {
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      columns: this.createTableColumns(),
      topFilterList: this.createTopFilterList(),
      fetchParams: {},
      tableDataList: [],
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    console.log('子页面参数:', this.rowData);
    this.columns.forEach(x => {
      x.hidden = true;
    });
    this.getWxlxSelect();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'MULTIPLE_SELECT',
          label: '汇总方式',
          fieldName: 'summaryType',
          itemList: [
            { text: '服务顾问', value: 'LAST_CONSULTANT_ID' },
            { text: '底盘号', value: 'VIN' },
            { text: '维修类型', value: 'REPAIR_TYPE_CODE' },
            { text: '最后进店日期', value: 'LAST_MAINTAIN_DATE' },
            { text: '首次进店日期', value: 'FIRST_IN_DATE' },
            { text: '首保日期', value: 'FIRST_MAINTAIN_DATE' },
            { text: '购车日期', value: 'SALES_DATE' },
            { text: '车型', value: 'MODEL_NAME' },
            { text: '是否本店售车', value: 'IS_SELF_COMPANY' },
            { text: '客户地址', value: 'ADDRESS' }
          ]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '服务顾问',
          dataIndex: 'LAST_CONSULTANT_ID',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '底盘号',
          dataIndex: 'VIN',
          sorter: true,
          filterType: 'input',
          filter: true,
          showOverflowTooltip: true
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_CODE',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: [],
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '最后进店日期',
          dataIndex: 'LAST_MAINTAIN_DATE',
          sorter: true,
          filter: true,
          filterType: 'date',
          showOverflowTooltip: true
        },
        {
          title: '首次进店日期',
          dataIndex: 'FIRST_IN_DATE',
          sorter: true,
          filter: true,
          filterType: 'date',
          showOverflowTooltip: true
        },
        {
          title: '首保日期',
          dataIndex: 'FIRST_MAINTAIN_DATE',
          sorter: true,
          filter: true,
          filterType: 'date',
          showOverflowTooltip: true
        },
        {
          title: '购车日期',
          dataIndex: 'SALES_DATE',
          sorter: true,
          filter: true,
          filterType: 'date',
          showOverflowTooltip: true
        },
        {
          title: '车型',
          dataIndex: 'MODEL_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '是否本店售车',
          dataIndex: 'IS_SELF_COMPANY',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '客户地址',
          dataIndex: 'ADDRESS',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '汇总数量',
          dataIndex: 'SUMMARY',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
      ];
    },
    async changeHandler(val) {
      this.$refs.table.START_LOADING();
      val.params = val.summaryType.toString();
      console.log('changeHandle', val);
      this.columns.forEach(x => {
        x.hidden = true;
      });
      val.beginDate = this.rowData.beginDate;
      val.endDate = this.rowData.endDate;
      val.beginDate1 = this.rowData.beginDate1;
      val.endDate1 = this.rowData.endDate1;
      this.columns.forEach(x => {
        val.summaryType.includes(x.dataIndex) && (x.hidden = false);
      });
      this.columns.find(x => x.dataIndex == 'SUMMARY').hidden = false;
      this.fetchParams = Object.assign({}, val);
      const res = await getCustomerSummary(this.fetchParams);
      this.tableDataList = res.data;
      this.$refs.table.STOP_LOADING();
    },
    // 获取维修类型方法
    async getWxlxSelect() {
      const res = await getWxlxSelect();
      try {
        this.columns.find(item => item.dataIndex === 'REPAIR_TYPE_CODE').editItems = res.data.map(x => ({
          value: x.REPAIR_TYPE_CODE,
          text: x.REPAIR_TYPE_NAME
        }));
        this.columns.find(item => item.dataIndex === 'REPAIR_TYPE_CODE').filterItems = res.data.map(x => ({
          value: x.REPAIR_TYPE_CODE,
          text: x.REPAIR_TYPE_NAME
        }));
      } catch (err) {
        console.log(err);
      }
    },
    // 适配 BaseTable 组件
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    tableAllData(val) {
      console.log('tableDate', val);
    },
    selectChangeHandle(val) {
      this.selectedRow = val;
    },
    closeDialog() {
      this.$emit('close', false);
    }
  }
};
</script>
<style lang="less" scoped></style>
